package com.springboot.last_experiment.controller;


import com.alibaba.fastjson.JSONObject;
import com.springboot.last_experiment.commmon.CusAccessObjectUtil;
import com.springboot.last_experiment.commmon.TCaptchaVerify;
import com.springboot.last_experiment.entity.User;
import com.springboot.last_experiment.service.UserService;
import com.springboot.last_experiment.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value ="/loginAuthen", method = {RequestMethod . POST, RequestMethod. GET})
    public JSONObject loginAuthen (HttpServletRequest request ,
                                   HttpServletResponse response , ModelMap modelMap) throws Exception {
        String ticket = request . getParameter("ticket");
        String randstr = request . getParameter("randstr");
        String userIp = CusAccessObjectUtil.getIpAddr(request);
        JSONObject json0bject = TCaptchaVerify.verifyTicket(ticket,randstr,userIp);
        return json0bject;
    }

    @PostMapping("user/login")
    public ResultVO login(@RequestParam(value = "code", required = false) String code,
                          @RequestParam(value = "rawData", required = false) String rawData,
                          @RequestParam(value = "signature", required = false) String signature
    ) {
        return userService.user_login(code,rawData,signature);
    }

    /**
     * 登录后完善信息
     * @param user
     * @return
     */
    @PutMapping("user/register")
    public ResultVO register(User user){
        return userService.user_register(user);
    }

    @PutMapping("user")
    public ResultVO update(User user){
        return userService.user_update(user);
    }

    @GetMapping("user/{openid}")
    public  ResultVO selectById(@PathVariable("openid") String openid,
                                @RequestParam("skey") String skey){

        return userService.user_selectById(openid,skey);
    }
    @GetMapping("user")
    public  ResultVO selectUserById(@RequestParam("openid") String openid){
        System.out.println(openid);
        return userService.user_selectById(openid);
    }
    @GetMapping("users")
    public ResultVO selectAll(){
        return userService.selectAllUser();
    }
    /**
     * 搜索：姓名或房号的模糊查询，根据身份证搜索
     * @param key
     * @return
     */
    @GetMapping("/users/{k}")
    public ResultVO getUserByKey(@PathVariable("k") String key){
        return userService.getUserByKey(key);
    }
    /**
     * 修改用户身份
     * @param openid
     * @return
     */
    @PutMapping("/user/role/{openid}")
    public ResultVO updateRole(@PathVariable("openid") String openid){
        return userService.updateRole(openid);
    }

    /**
     * 锁定指定账号
     * @param openid
     * @return
     */
    @PutMapping("/user/lock/{openid}")
    public ResultVO lockRes(@PathVariable("openid") String openid){
        return userService.lockUser(openid);
    }

    /**
     * 修改用户状态
     * @param openid
     * @param status
     * @return
     */
    @PutMapping("/user/status")
    public ResultVO updateStatus(@RequestParam("openid") String openid,@RequestParam("status") Integer status){
        return userService.updateStatus(openid,status);
    }

    /**
     * 修改账户的审核情况
     */
    @PutMapping("/user/check")
    public ResultVO updateCheck(@RequestParam("openid") String openid,@RequestParam("checked") Integer check){

        return userService.updateCheck(openid,check);
    }
}
